//
// Created by lymk on 2022/2/26.
//

#include "Camera.h"

glm::mat4 Camera::GetViewPortMatrix() const {
    glm::mat4 result = glm::mat4(1.0f);
    result[0][0] = Right.x;
    result[1][0] = Right.y;
    result[2][0] = Right.z;
    result[3][0] = -glm::dot(Right, Position);
    result[0][1] = Up.x;
    result[1][1] = Up.y;
    result[2][1] = Up.z;
    result[3][1] = -glm::dot(Up, Position);
    result[0][2] = -Front.x;
    result[1][2] = -Front.y;
    result[2][2] = -Front.z;
    result[3][2] = glm::dot(Front, Position);
    return result;
}

glm::mat4 Camera::GetPerspectiveMatrix() const {
    glm::mat4 result = glm::mat4(0.0f);
    const float tanHalfFov = tan(Fov*0.5f);
    result[0][0] = 1.0f / (Aspect*tanHalfFov);
    result[1][1] = 1.0f / (tanHalfFov);
    result[2][2] = -(Far + Near) / (Far - Near);
    result[2][3] = -1.0f;
    result[3][2] = (-2.0f*Near*Far) / (Far - Near);

    return result;
}

Camera::Camera() {
    Position = {0,0,5};
    Up = {0.f,1.f,0.f};
    Front = {0.f,0.f,-1.f};
    Right = {1.f,0,0.f};
    Fov = 1.f;
    Aspect = 1.f;
    Near = 1.f;
    Far = 10.f;
}
